/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import com.cmcc.zysoft.infocollection.model.Industry;
import com.starit.common.dao.hibernate.HibernateBaseDaoImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: IndustryDao.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-14 下午4:03:16
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Repository
public class IndustryPcDao extends HibernateBaseDaoImpl<Industry, String> {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 获取行业分页列表信息.
	 * @param page
	 * @param rows
	 * @param keyword 搜索关键字
	 * @return map
	 */
	public Map<String, Object> getIndustryList(int page, int rows, String keyword) {
		int offset = (page - 1) * rows;
		Map<String, Object> map = new HashMap<String, Object>();
		String rowSql = 
				" SELECT a.industry_id AS industryId, a.industry_name AS industryName, a.industry_life AS industryLife, " +
				" a.mark1 AS mark1, a.mark2 AS mark2, a.mark3 AS mark3, a.mark4 AS mark4, " +
				" ( SELECT b.channel_name FROM tb_b_channel b WHERE a.channel_id = b.channel_id ) AS channelName " +
				" FROM tb_b_industry a ";
		String countSql = " SELECT COUNT(*) FROM tb_b_industry a ";
		if(StringUtils.hasText(keyword)) {
			rowSql += " WHERE a.industry_name LIKE '%" + keyword.trim() + "%' ";
			countSql += " WHERE a.industry_name LIKE '%" + keyword.trim() + "%' ";
		}
		rowSql += " ORDER BY a.channel_id LIMIT ?, ? ";
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(rowSql, offset, rows);
		int total = this.jdbcTemplate.queryForInt(countSql);
		map.put("rows", list);
		map.put("total", total);
		return map;
	}
	
	/**
	 * 检查行业名称是否已经存在.
	 * @param industryName 行业名称
	 * @param industryId 行业ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> check(String industryName, String industryId) {
		String sql = "SELECT a.* FROM tb_b_industry a WHERE a.industry_name = ? ";
		if(StringUtils.hasText(industryId)) {
			sql += " AND a.industry_id != '" + industryId + "'";
		}
		return this.jdbcTemplate.queryForList(sql, industryName);
	}
	
	/**
	 * 根据频道ID查找行业列表信息.
	 * @param channelId 频道ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> industrys(String channelId) {
		String sql = 
				" SELECT a.industry_id AS industryId, a.industry_name AS industryName, a.industry_life AS industryLife " +
				" FROM tb_b_industry a WHERE a.channel_id = ?";
		return this.jdbcTemplate.queryForList(sql, channelId);
	}
}